package vip.xiaonuo.modular.threemessagetable.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.core.util.DateUtils;
import vip.xiaonuo.modular.threemessagetable.entity.ThreeMessageTable;
import vip.xiaonuo.modular.threemessagetable.mapper.ThreeMessageTableMapper;
import vip.xiaonuo.modular.threemessagetable.service.ThreeMessageTableService;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class CleanThreeMessageTask implements TimerTaskRunner {

    @Autowired
    private ThreeMessageTableService threeMessageTableService;

    @Resource
    private ThreeMessageTableMapper messageTableMapper;

    @Override
    public void action(String taskId, String taskParam) {
        execute();
    }

    private void execute() {
        log.info("开始删除30天之前的接收三方消息日志！");
        Date date = DateUtils.addDay(new Date(), -30);
        threeMessageTableService.remove(new LambdaQueryWrapper<ThreeMessageTable>().le(ThreeMessageTable::getCreateTime, date));
        messageTableMapper.deleteMessageTable(date);
    }

    @Override
    @XxlJob("CleanLogTask")
    public void executeXxlJob() {
        execute();
    }
}
